package com.aaa.controller;

import com.aaa.entity.T_Cp;
import com.aaa.service.T_CpService;
import com.aaa.util.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: TT
 * @Date: 2019/3/30 15:55
 * @Version 1.0
 */
@Controller
@RequestMapping("/ceping/")
public class T_CpController {
    private List<T_Cp>  cp= null;
@Autowired
private T_CpService service;
    @RequestMapping("html")
    public String html() {
        return "statistics/ceping";
    }
    @RequestMapping("select")
    @ResponseBody
    public Map selectScorce(@RequestBody Map map){
        List<Map> data=service.getlist(map);
        Integer total=service.Count(map);
        Map map1=new HashMap();
        map1.put("data",data);
        map1.put("total",total);
        return map1;
    }
    @RequestMapping("data")
    @ResponseBody
    public int excelKouFen(@RequestBody Map map) {
            cp = new ArrayList<T_Cp>();
        List<Map> list = (List) map.get("xinxi");
        for (int i = 0; i < list.size(); i++) {
           T_Cp cp1=new T_Cp();

            cp1.setName((String) list.get(i).get("name"));
            cp1.setSHtime((String) list.get(i).get("SHtime"));
            cp1.setKscore((Integer) list.get(i).get("kscore"));
            cp.add(cp1);
            System.out.println(list.get(i));
        }
        return 1;
    }
    @RequestMapping("export")
    @ResponseBody
    public void export(HttpServletRequest request, HttpServletResponse response) throws Exception {
        //获取数据
        List<T_Cp> list = cp;
        //excel标题
        String[] title = {"单位名称", "分数", "申报时间"};

        //excel文件名
        String fileName = "申报信息表" + System.currentTimeMillis() + ".xls";

        //sheet名
        String sheetName = "申报信息表";
        String[][] content = new String[list.size()][];
        for (int i = 0; i < list.size(); i++) {
            content[i] = new String[title.length];
            T_Cp obj = list.get(i);
            content[i][0] = obj.getName();
            content[i][1] = String.valueOf(obj.getKscore());
            content[i][2] = obj.getSHtime();


            //创建HSSFWorkbook
            HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

            //响应到客户端
            try {
                this.setResponseHeader(response, fileName);
                OutputStream os = response.getOutputStream();
                wb.write(os);
                os.flush();
                os.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    //发送响应流方法
    @SuppressWarnings("all")
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
